package deliciouse.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import deliciouse.dao.DaoException;
import deliciouse.dao.FoodDao;
import deliciouse.entity.Food;

public class FoodDaoImpl implements FoodDao {
	private static class FoodRowMapper implements RowMapper<Food> {
		public Food mapRow(ResultSet rs) throws SQLException {
			Food f = new Food();
			f.setId(rs.getInt("Id"));
			f.setChineseName(rs.getString("chineseName"));
			f.setEnglishName(rs.getString("englishName"));
			f.setCategoryId(rs.getInt("categoryId"));
			f.setDescription(rs.getString("description"));
			f.setImageFile(rs.getString("imageFile"));
			return f;
		}
	}

	private JdbcTemplate db = new JdbcTemplate();

	public List<Food> getAll() {
		return db.queryList("select * from foods limit 6", new FoodRowMapper());
	}

	@Override
	public Food fetchById(int id) {
		return db.query("select * from foods where id =?", new FoodRowMapper(), id);
	}

	public static void main(String[] args) {
		int id = 2;
		FoodDao food = new FoodDaoImpl();
		Food f = food.fetchById(id);
		System.out.println(f.getDescription());
		//System.out.println(food.getAllRows());

	}

	@Override
	public List<Food> fetchByCondition(int cid, String title, int pageNum, int pageSize) {
		ArrayList parameters = new ArrayList();
		String sql = "select * from foods where 1=1 ";
		if (cid > 0) {
			sql += " and CategoryId =?";
			parameters.add(cid);
		}
		if (title != null && !title.isEmpty()) {
			sql += " and chinesename like ?";
			parameters.add("%" + title + "%");
		}

		sql += " limit ?,?";
		parameters.add((pageNum - 1) * pageSize);
		parameters.add(pageSize);
		return db.queryList(sql, new FoodRowMapper(), parameters.toArray());
	}

	@Override
	public int getAllRows(int cid, String title) {
		ArrayList parameters = new ArrayList();
		int rows = 0;
		String sql = "SELECT COUNT(id) FROM foods where 1=1";
		if (cid > 0) {
			sql += " and CategoryId =?";
			parameters.add(cid);
		}
		if (title != null && !title.isEmpty()) {
			sql += " and chinesename like ?";
			parameters.add("%" + title + "%");
		}
		return rows = (int) db.query(sql, new RowMapper() {
			public Object mapRow(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}

		},parameters.toArray());
	}

	@Override
	public boolean addfoodlist(Food food) {
		ArrayList parameters = new ArrayList();
		parameters.add(food.getId());
		parameters.add(food.getChineseName());
		parameters.add(food.getEnglishName());
		parameters.add(food.getCategoryId());
		parameters.add(food.getDescription());
		parameters.add(food.getImageFile());
		String sql="insert into foods (id,chinesename,englishname,CategoryId,description,imagefile) values(?,?,?,?,?,?)";
		db.executeUpdate(sql, parameters.toArray());
		return true;
	}

	@Override
	public boolean updatefoodlist(Food food,int id) {
		ArrayList parameters = new ArrayList();
		parameters.add(food.getId());
		parameters.add(food.getChineseName());
		parameters.add(food.getEnglishName());
		parameters.add(food.getCategoryId());
		parameters.add(food.getDescription());
		parameters.add(food.getImageFile());
		parameters.add(id);
		String sql="UPDATE foods SET Id=?,chinesename =?, englishname=?, CategoryId= ?, description=?, imagefile=? WHERE id=?";
		db.executeUpdate(sql, parameters.toArray());
		return true;
	}

	/*
	 * @Override public int getAllRows() { int rows =0; String sql
	 * ="SELECT COUNT(id) FROM foods"; Connection conn = null; PreparedStatement
	 * stmt = null; ResultSet rs = null; try{ conn=db.openConnection(); stmt =
	 * conn.prepareStatement(sql); rs = stmt.executeQuery(); if(rs.next()){ rows
	 * =rs.getInt(1); } }catch(Exception e){ throw new DaoException(); }finally{
	 * db.closeResx(rs, stmt, conn); }
	 * 
	 * return rows; }
	 */

}
